package com.tomehay.termprojectmanage_server.service.impl;

import com.tomehay.termprojectmanage_server.enums.ResponseCode;
import com.tomehay.termprojectmanage_server.mapper.GroupMemberMapper;
import com.tomehay.termprojectmanage_server.mapper.StudentMapper;
import com.tomehay.termprojectmanage_server.pojo.Model.Allocation;
import com.tomehay.termprojectmanage_server.mapper.AllocationMapper;
import com.tomehay.termprojectmanage_server.pojo.Model.GroupMember;
import com.tomehay.termprojectmanage_server.pojo.VO.AllocationVO;
import com.tomehay.termprojectmanage_server.pojo.VO.ResultVO;
import com.tomehay.termprojectmanage_server.service.AllocationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tomehay
 * @since 2023-06-20
 */
@Service
public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocation> implements AllocationService {

    @Resource
    AllocationMapper allocationMapper;

    @Resource
    GroupMemberMapper groupMemberMapper;

    @Resource
    StudentMapper studentMapper;

    @Override
    public ResultVO<Void> AllocateTermProject(int groupId, int termProjectId) {
        Allocation allocation = new Allocation();
        allocation.setTermProjectId(termProjectId);
        allocation.setGroupId(groupId);
        allocation.setCreateTime(LocalDateTime.now());
        allocationMapper.insert(allocation);
        return new ResultVO<>(ResponseCode.OK);
    }

    @Override
    public ResultVO<Allocation> getOneAllocate(int id) {
        Allocation allocation = allocationMapper.selectById(id);

        return new ResultVO<>(ResponseCode.auto(allocation),allocation);
    }

    @Override
    public ResultVO<List<AllocationVO>> getAllocations(int courseId) {
        List<AllocationVO> allocationVOS = allocationMapper.getAllocationVO(courseId);
        for (AllocationVO allocationVO : allocationVOS) {
            allocationVO.setGroupMembers(
                    GroupMember.GetMembers(groupMemberMapper,
                            allocationVO.getGroupId(),
                            studentMapper)
            );
        }
        return new ResultVO<>(ResponseCode.auto(allocationVOS),allocationVOS);
    }
}
